package health_system.health_system.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import health_system.health_system.entity.MomentComment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MomentCommentDTO {
    private Long id;
    private Long momentId;
    private Long userId;
    private String username;
    private String userAvatarUrl;
    private String content;
    private Long parentId;
    private Long replyToUserId;
    private String replyToUsername;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private Integer replyCount;
    private List<MomentCommentDTO> replies;
    
    public static MomentCommentDTO fromEntity(MomentComment comment) {
        if (comment == null) {
            return null;
        }
        
        MomentCommentDTO dto = new MomentCommentDTO();
        dto.setId(comment.getId());
        
        if (comment.getMoment() != null) {
            dto.setMomentId(comment.getMoment().getId());
        }
        
        if (comment.getUser() != null) {
            dto.setUserId(comment.getUser().getId());
            dto.setUsername(comment.getUser().getUsername());
            dto.setUserAvatarUrl(comment.getUser().getAvatarUrl());
        }
        
        dto.setContent(comment.getContent());
        
        if (comment.getParent() != null) {
            dto.setParentId(comment.getParent().getId());
        }
        
        if (comment.getReplyToUser() != null) {
            dto.setReplyToUserId(comment.getReplyToUser().getId());
            dto.setReplyToUsername(comment.getReplyToUser().getUsername());
        }
        
        dto.setCreatedAt(comment.getCreatedAt());
        dto.setUpdatedAt(comment.getUpdatedAt());
        
        return dto;
    }
    
    public static MomentCommentDTO fromEntityWithReplies(MomentComment comment, List<MomentCommentDTO> replies) {
        MomentCommentDTO dto = fromEntity(comment);
        if (dto != null) {
            dto.setReplies(replies);
            dto.setReplyCount(replies != null ? replies.size() : 0);
        }
        return dto;
    }
} 